<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="试题名称" prop="questionName">
        <el-input
          v-model="dataForm.questionName"
          placeholder="试题名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="选项A" prop="sectionOne">
        <el-input v-model="dataForm.sectionOne" placeholder="选项A"></el-input>
      </el-form-item>
      <el-form-item label="选项B" prop="sectionTwo">
        <el-input v-model="dataForm.sectionTwo" placeholder="选项B"></el-input>
      </el-form-item>
      <el-form-item label="选项C" prop="sectionThree">
        <el-input
          v-model="dataForm.sectionThree"
          placeholder="选项C"
        ></el-input>
      </el-form-item>
      <el-form-item label="选项D" prop="sectionFour">
        <el-input v-model="dataForm.sectionFour" placeholder="选项D"></el-input>
      </el-form-item>
      <el-form-item label="正确答案" prop="correctAnswer">
        <el-input
          v-model="dataForm.correctAnswer"
          placeholder="正确答案"
        ></el-input>
      </el-form-item>
      <el-form-item label="试题分数" prop="questionScore">
        <el-input
          v-model="dataForm.questionScore"
          placeholder="试题分数"
        ></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: 0,
        questionName: "",
        sectionOne: "",
        sectionTwo: "",
        sectionThree: "",
        sectionFour: "",
        correctAnswer: "",
        questionScore: "",
        createTime: "",
        updateTime: "",
        isDelete: "",
      },
      dataRule: {
        questionName: [
          { required: true, message: "试题名称不能为空", trigger: "blur" },
        ],
        sectionOne: [
          { required: true, message: "选项A不能为空", trigger: "blur" },
        ],
        sectionTwo: [
          { required: true, message: "选项B不能为空", trigger: "blur" },
        ],
        sectionThree: [
          { required: true, message: "选项C不能为空", trigger: "blur" },
        ],
        sectionFour: [
          { required: true, message: "选项D不能为空", trigger: "blur" },
        ],
        correctAnswer: [
          { required: true, message: "正确答案不能为空", trigger: "blur" },
        ],
        questionScore: [
          { required: true, message: "试题分数不能为空", trigger: "blur" },
        ],
        createTime: [
          { required: false, message: "创建时间不能为空", trigger: "blur" },
        ],
        updateTime: [
          { required: false, message: "更新时间不能为空", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.id = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(
              `/exam/questionanswer/info/${this.dataForm.id}`
            ),
            method: "get",
            params: this.$http.adornParams(),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.dataForm.questionName = data.questionAnswer.questionName;
              this.dataForm.sectionOne = data.questionAnswer.sectionOne;
              this.dataForm.sectionTwo = data.questionAnswer.sectionTwo;
              this.dataForm.sectionThree = data.questionAnswer.sectionThree;
              this.dataForm.sectionFour = data.questionAnswer.sectionFour;
              this.dataForm.correctAnswer = data.questionAnswer.correctAnswer;
              this.dataForm.questionScore = data.questionAnswer.questionScore;
              this.dataForm.createTime = data.questionAnswer.createTime;
              this.dataForm.updateTime = data.questionAnswer.updateTime;
              this.dataForm.isDelete = data.questionAnswer.isDelete;
            }
          });
        }
      });
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/exam/questionanswer/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              questionName: this.dataForm.questionName,
              sectionOne: this.dataForm.sectionOne,
              sectionTwo: this.dataForm.sectionTwo,
              sectionThree: this.dataForm.sectionThree,
              sectionFour: this.dataForm.sectionFour,
              correctAnswer: this.dataForm.correctAnswer,
              questionScore: this.dataForm.questionScore,
              createTime: this.dataForm.createTime,
              updateTime: this.dataForm.updateTime,
              isDelete: this.dataForm.isDelete,
            }),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        }
      });
    },
  },
};
</script>
